<?php
namespace Admin\Controller;

class NodeController extends AdminController
{
    public function index()
    {
        $this->display();
    }
    public function view()
    {
        $n = M("node");
        import("ORG.Util.Page"); //导入分页类
        $count = $n->count(); //计算总数
        $list = $n->order('sort asc')->select();
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        echo ($list); //输出json数据
    }
    public function viewlist()
    {
        $n = M("node");
        import("ORG.Util.Page"); //导入分页类
        $count = $n->count(); //计算总数
        $list = $n->cache(60*60*12)->order('sort asc')->select();
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        return ($list); //输出json数据
    }
    //添加提交处理
    public function add()
    {
        if (empty($_POST)) {
            $Node = A('Node');
            $nodelist = $Node->viewlist();
            $this->assign('nodelist', $nodelist);
            $this->display();
        } else {
            $m = D("Node");
            if (!$m->create()) {
                $this->error($m->getError(), 3);
            } else {
                if ($result = $m->add()) {
                    $this->success('添加成功！', 1);
                } else {
                    $this->error('添加失败！', 0);
                }
            }
        }
    }
    // 删除数据
    public function delete()
    {
        if (!empty($_GET['id'])) {
            $d = D("node");
            $condition['id'] = array('in', $_GET['id']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $this->success('删除成功！', 1);
            } else {
                $this->error('删除出错！', 1);
            }
        } else {
            $this->error('删除项不存在！', 1);
        }
    }
    // 更新数据
    public function edit()
    {
        if (empty($_POST)) {
            $Node = A('Node');
            $nodelist = $Node->viewlist();
            $this->assign('nodelist', $nodelist);
            $this->display();
        } else {
            $Form = D("Node");
            if ($vo = $Form->create($_POST)) {
                $list = $Form->save($vo);
                if ($list) {
                    $this->success('更新成功！', 1);
                } else {
                    $this->error('没有更新任何数据!', 0);
                }
            } else {
                $this->error($Form->getError(), 3);
            }
        }
    }
}
